package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.ShopAdapter;
import com.luck.easyfoodbatch.mvp.home.ScanGoodSearchModel;
import com.luck.easyfoodbatch.mvp.home.ScanGoodSearchPresenter;
import com.luck.easyfoodbatch.mvp.home.ScanGoodSearchView;
import com.luck.easyfoodbatch.mvp.model.SearchGoods;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

public class ScanActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, ScanGoodSearchView {
    private TextView tv_back;
    private ImageView iv_nodata;
    private EasyRecyclerView mRecyclerView;
    private ShopAdapter adapter;
    private List<SearchGoods.DataBean.ModelsBean> list = new ArrayList<>();
    private String bar_code = "";
    private String p_id = "";
    private String shopUid = "";
    private ScanGoodSearchModel mScanGoodSearchModel;//扫一扫
    private boolean isLoading = false;
    private int isRefresh = 0;
    private int page = 1;
    private int totalPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        initView();
    }

    private void initView() {
        App.getInstance().addActivity(ScanActivity.this);
        tv_back = (TextView) findViewById(R.id.tv_back);
        mRecyclerView = (EasyRecyclerView) findViewById(R.id.recyclerView);
        iv_nodata= (ImageView) findViewById(R.id.iv_nodata);
        p_id = (String) SharedPreferencesUtil.getData(ScanActivity.this, HttpUtil.P_ID, "3");
        bar_code = getIntent().getStringExtra("scan_result");
        mScanGoodSearchModel = new ScanGoodSearchPresenter(this, mContext);
        if (Tools.isNull(bar_code)) {
            bar_code = "";
        }
        if (Tools.isNull(p_id)) {
            p_id = "";
        }
        isRefresh = 0;
        showProgressDialog("正在搜索，请稍候……");
        mScanGoodSearchModel.searchGoodsByScan(page, bar_code, p_id);

        /**
         * 商品
         */
        LinearLayoutManager gridManager = new LinearLayoutManager(mContext);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        mRecyclerView.setLayoutManager(gridManager);
        adapter = new ShopAdapter(mContext);
        mRecyclerView.setAdapterWithProgress(adapter);
        adapter.setMore(R.layout.view_more, this);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                SearchGoods.DataBean.ModelsBean bean = list.get(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("gid", bean.getGid() + "");
                intent.putExtras(bundle);
                intent.setClass(mContext, ShopDetailsActivity.class);
                startActivityForResult(intent, 300);
            }
        });
        adapter.addAll(list);
        mRecyclerView.setAdapter(adapter);
        if (list != null) {
            tv_back.setText("搜索结果");
            adapter.setNoMore(R.layout.view_nomore);
        }
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void getScanGoodSearchSuccess(SearchGoods model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                SearchGoods.DataBean data = model.getData();
                if (data != null) {
                    list.addAll(model.getData().getModels());
                   /* // 下拉刷新 清空集合
                    switch (isRefresh) {
                        case 0:
                            list = model.getData().getModels();
                            break;
                        case 1:
                            // 下拉刷新
                            list = model.getData().getModels();
                            break;
                        case 2:
                            // 上拉加载
                            list.addAll(model.getData().getModels());
                            break;
                    }*/
                } else {
                    list = new ArrayList<>();
                }
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
            } else {
//                mRecyclerView.setVisibility(View.GONE);
                iv_nodata.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void httpStatusError() {
        showToast("网络连接不顺,请检查网络");
        mRecyclerView.setVisibility(View.GONE);
    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }

    @Override
    public void onLoadMore() {
        //加载
        isRefresh = 2;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }
        mScanGoodSearchModel.searchGoodsByScan(page, bar_code, p_id);
    }

    /**
     * 加载数据
     */
    public void loadingData() {
        if (!isLoading) {
            showProgressDialog("正在搜索，请稍候……");
            mScanGoodSearchModel.searchGoodsByScan(page, bar_code, p_id);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 300:
                    finish();
                    break;
            }
        }
    }

    @Override
    public void onRefresh() {
        //刷新
        isRefresh = 1;
        list.clear();
        mScanGoodSearchModel.searchGoodsByScan(page, bar_code, p_id);
    }
}
